﻿using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System;
using System.Linq;
using System.Text;
/*http://www.java2s.com/Tutorial/CSharp/0540__XML/SpecifytheXmlRootandXmlAttributeforXMLserialization.htm */
namespace ConvertObjectToXML
{
    [XmlRoot("EmployeeList")]
    public class EmployeeList
    {
        private ArrayList employeeList = new ArrayList();

        public EmployeeList()
        {
        }

        [XmlElement("employee")]
        public Employee[] Employees
        {
            get
            {
                Employee[] employees = new Employee[employeeList.Count];
                employeeList.CopyTo(employees);
                return employees;
            }
            set
            {
                Employee[] employees = (Employee[])value;
                employeeList.Clear();
                foreach (Employee i in employees)
                    employeeList.Add(i);
            }
        }

        public void AddEmployee(Employee employee)
        {
            employeeList.Add(employee);
        }

        public Employee this[string firstName]
        {
            get
            {
                foreach (Employee i in employeeList)
                    if (i.firstName == firstName)
                        return i;
                throw (new Exception("Employee not found"));
            }
        }

        public void DisplayEmployees()
        {
            foreach (Employee i in employeeList)
                Console.WriteLine(i);
        }

    }

    public class Employee
    {
        [XmlAttribute("firstName")]
        public string firstName;
        [XmlAttribute("lastName")]
        public string lastName;
        [XmlAttribute("salary")]
        public double salary;
        [XmlAttribute("number")]
        public int number;


        public Employee() { }

        public Employee(string f, string l, double s, int n)
        {
            firstName = f;
            lastName = l;
            salary = s;
            number = n;
        }

        public override string ToString()
        {
            object[] o = new object[] { firstName, lastName, salary, number };
            return string.Format("{0,-5} {1,-10} ${2,5:#,###.00} {3,3}", o);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            EmployeeList employList = new EmployeeList();

            employList.AddEmployee(new Employee("A", "p", 123.15, 100));
            employList.AddEmployee(new Employee("B", "c", 712.50, 25));
            employList.AddEmployee(new Employee("C", "wt", 132.35, 10));

            employList.DisplayEmployees();
            Console.WriteLine("Serialization in progress");
            XmlSerializer s = new XmlSerializer(typeof(EmployeeList));
            TextWriter w = new StreamWriter("employList.xml");
            s.Serialize(w, employList);
            w.Close();
            Console.WriteLine("Serialization complete\n\n");

            Console.WriteLine("Deserialization in progress");
            EmployeeList employList2 = new EmployeeList();
            TextReader r = new StreamReader("employList.xml");
            employList2 = (EmployeeList)s.Deserialize(r);
            r.Close();
            Console.WriteLine("Deserialization complete");
            employList2.DisplayEmployees();
        }
    }
}
